import { Injectable } from '@angular/core';
import {MsgResult} from '../../../models/msg-result';
import {Observable} from 'rxjs/Rx';
import {RequestUtil} from '../../../util/request-util';
import {FormUtil} from '../../../util/form-util';
import {SiteV2API} from '../site-v2-constants';
import {DeviceCategory} from "./device-category";

@Injectable()
export class DeviceCategoryService {

  constructor(
    private formUtil: FormUtil,
    private requestUtil: RequestUtil
  ) { }
  /**
   * 通过ID获取设备类型
   * @param id :设备类型
   * @returns {Observable<MsgResult>|any}
   */
  getDeviceCateByCode(monitorCode): Observable<MsgResult> {
    return this.requestUtil.get(SiteV2API.getDeviceCateByCode.replace('{monitorCode}', monitorCode));
  }
  //添加设备类型
  insertDeviceCate(deviceCategory : DeviceCategory):Observable<MsgResult>{
    let params = this.formUtil.serialize(deviceCategory);
    return this.requestUtil.post(SiteV2API.insertDeviceCate,params);
  }
  /**
   * 设备类型
   * @param siteCate: 设备类型数据
   * @returns {Observable<MsgResult>|any}
   */
  updateDeviceCate(oldCode : string,deviceCategory : DeviceCategory): Observable<MsgResult> {
    const params = this.formUtil.serialize(deviceCategory);
    return this.requestUtil.put(SiteV2API.updateDeviceCate.replace('{monitorCode}', oldCode), params.replace('monitorCode','newCode'));
  }
  /**
   * 删除设备类型
   * @param id :设备类型
   * @returns {Observable<MsgResult>|any}
   */
  deleteDeviceCate(monitorCode): Observable<MsgResult> {
    return this.requestUtil.delete(SiteV2API.deleteDeviceCateByCodes.replace('{monitorCode}', monitorCode));
  }

  //批量删除设备类型
  deviceCateDeletes(ids : Array<string>):Observable<MsgResult>{
    return this.requestUtil.post(SiteV2API.deleteDeviceCateByCodes.replace('{monitorCode}', ids.join(',')));
  }

}
